package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class TodayBestService {

    @Autowired
    private UserService userService;

    @Autowired
    private RecommendUserService recommendUserService;

    @Autowired
    private UserInfoService userInfoService;

    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;

    public TodayBest queryTodayBest() {

        User user = UserThreadLocal.get();

        //查询今日佳人
        TodayBest todayBest = recommendUserService.queryTodayBest(user.getId());
        //如果查询不到会给出默认佳人
        if(null == todayBest){
            todayBest = new TodayBest();
            todayBest.setId(defaultUser);
            todayBest.setFateValue(80l);
        }

        UserInfo userInfo = userInfoService.queryUserInfoByUserId(todayBest.getId());
        if (null == userInfo){
            return null;
        }
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setTags(StringUtils.split(userInfo.getTags(), ','));
        todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
        todayBest.setAge(userInfo.getAge());

        return todayBest;
    }

    public PageResult queryRecommendation( RecommendUserQueryParam queryParam) {
        User user = UserThreadLocal.get();

        PageResult pageResult = new PageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());

        PageInfo<RecommendUser> pageInfo= recommendUserService.queryRecommendation(user.getId(),
                queryParam.getPage(),queryParam.getPagesize());

        List<RecommendUser> records = pageInfo.getRecords();

        if(CollectionUtils.isEmpty(records)){
            //没有推荐列表
            return pageResult;
        }

        //返回信息的填充

        //收集所有推荐列表的id
        Set<Long> userIds = new HashSet<>();
        for (RecommendUser record : records) {
            userIds.add(record.getUserId());
        }

        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();

        wrapper.in("user_id",userIds);

//        if(StringUtils.isNotEmpty(queryParam.getGender())){
//            wrapper.eq("sex",StringUtils.equals(queryParam.getGender(),"man") ? 1 :2);
//        }

        if (StringUtils.isNotEmpty(queryParam.getCity())) {
            wrapper.like("city", queryParam.getCity());
        }

        //年龄小于等于
        if (queryParam.getAge() != null) {
            wrapper.le("age", queryParam.getAge());
        }

        List<UserInfo> userInfoList =  userInfoService.queryUserInfoList(wrapper);
        if(CollectionUtils.isEmpty(userInfoList)){
            //查询不到推荐列表
            return pageResult;
        }

        List<TodayBest> todayBestList = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            TodayBest todayBest = new TodayBest();

            todayBest.setId(userInfo.getUserId());
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setNickname(userInfo.getNickName());
            todayBest.setTags(StringUtils.split(userInfo.getTags(), ','));
            todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
            todayBest.setAge(userInfo.getAge());


            //缘分值取整
            for (RecommendUser record : records) {
                if (record.getUserId().longValue() == userInfo.getUserId().longValue()){
                    double score = Math.floor(record.getScore());
                    todayBest.setFateValue(Double.valueOf(score).longValue());
                    break;
                }
            }

            todayBestList.add(todayBest);

        }

        //按照缘分值降序排序
        Collections.sort(todayBestList, (o1, o2) -> new Long(o2.getFateValue() - o1.getFateValue()).intValue());


        pageResult.setItems(todayBestList);

        return pageResult;
    }


}
